<?php


namespace app\admin\model\product;


use core\basic\Model;

class OrderModel extends  Model
{
    // 获取订单列表
    public function getList()
    {
        $field = array(
            'a.*',
            'u.username',
            'p.product_name',
        );
        $join = [
            ['ay_member u','a.user_id=u.id', 'LEFT'],
            ['ay_product p','a.product_id=p.id', 'LEFT'],
        ];
        return parent::table('ay_order a')->field($field)
            ->join($join)
            ->order('a.id desc')
            ->page()
            ->select();


    }

    // 添加订单
    public function addOrder(array $data)
    {
        return parent::table('ay_order')->insert($data);
    }
    // 获取订单详情
    public function getOrder($id)
    {


        $field = array(
            'a.*',
            'u.username',
            'p.product_name ,p.product_pic,p.price',
        );
        $join = [
            ['ay_member u','a.user_id=u.id', 'LEFT'],
            ['ay_product p','a.product_id=p.id', 'LEFT'],
        ];
        return parent::table('ay_order a')->field($field)
            ->join($join)
            ->where(['a.id'=>$id])
            ->find();
    }
    // 查找订单
    public function findOrder($fields, $keyword)
    {
        $field = array(
            'a.*',
            'u.'.$fields,
            'p.product_name ,p.product_pic,p.price',
        );
        $join = [
            ['ay_member u','a.user_id=u.id', 'LEFT'],
            ['ay_product p','a.product_id=p.id', 'LEFT'],
        ];

        return parent::table('ay_order a')
            ->field($field)
            ->join($join)
            ->like($fields, $keyword)
            ->order('id')
            ->page()
            ->select();
    }
    // 删除订单
    public function delOrder($id)
    {
        return parent::table('ay_order')->where("id=$id")->delete();
    }

}